from flask import request
from . import menu_api
from flask_restful import Resource
from flask_shop import models

menu = models.Menus

class Menu(Resource):
    def get(self):
        #判断前端需要哪种数据类型
        type_name = request.args.get('type')
        if type_name=='tree':
            menu_list = menu.query.filter(menu.level==1).all()
            menu_data = []
            for  m in menu_list:
                menu_data.append(m.to_dict_tree())
            return {'status':200,'msg':'获取数据成功','data':menu_data}
        else:
            menu_list = menu.query.filter(menu.level != 0).all()
            menu_data = []
            for  m in menu_list:
                menu_data.append(m.to_dict_list())
            return {'status':200,'msg':'获取数据成功','data':menu_data}
        
menu_api.add_resource(Menu,'/menus/')
